import Vue from "vue";
import AppTip from "./app-tip.vue";
import { Subject } from 'rxjs';

export default class Tip{

    /**
     * 实例对象
     *
     * @private
     * @static
     * @memberof Tip
     */
    private static tip:any;
    
    /**
     * vue 实例
     *
     * @private
     * @type {Vue}
     * @memberof Tip
     */
    private vm!: Vue;


    /**
     * 单例构造函数
     * 
     * @memberof Tip
     */
    private constructor() {
        if (Tip.tip) {
            return Tip.tip;
        }
    }
    
    /**
     * 静态获取单例对象方法
     *
     * @static
     * @returns {Tip}
     * @memberof Tip
     */
    public static getInstance(): Tip {
        if (!Tip.tip) {
            Tip.tip = new Tip();
        }
        return Tip.tip;
    }
    
    /**
     * 弹出模态框
     *
     * @param {{componentName: string, target: any, position?: string, tipHeight?: number, tipWidth?: number, CustomStyle?: object}} props 
     * componentName:组件名 target:目标对象 position:提示框位置 tipHeight:提示框高度 tipWidth:提示框宽度 CustomStyle:自定义样式
     * @param {*} viewdata 视图数据对象
     * @returns {Subject<any>}
     * @memberof Tip
     */
    public openTip(props: {componentName: string, target: any, position?: string, tipHeight?: number, tipWidth?: number, CustomStyle?: object}, viewdata: any): Subject<any> {
        try {
            Object.assign(props,{viewdata:viewdata});
            let component = AppTip;
            const vm = new Vue({
                render(h) {
                    return h(component, { props });
                }
            }).$mount();
            this.vm = vm;
            // props.target.parentElement.appendChild(vm.$el);
            document.body.appendChild(vm.$el);
            const comp:any = vm.$children[0];
            return comp.show();
        } catch (error) {
            console.log(error);
            return new Subject<any>();
        }
    }

}